package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.ExistentInvoice;
import com.edward.model.InvoiceTrack;


public class InvoiceTrackManager extends HibernateDaoSupport {

	public void addInvoiceTrack(InvoiceTrack invoiceTrack) {
		
		getHibernateTemplate().save(invoiceTrack);
	}
	
	public void updateInvoiceTrack(InvoiceTrack invoiceTrack) {
		
		getHibernateTemplate().update(invoiceTrack);
	}
	
	public void deleteInvoiceTrack(int id){
		
		getHibernateTemplate().delete(getInvoiceTrack(id));
	}
	
	public InvoiceTrack getInvoiceTrack(int id){
		InvoiceTrack d =(InvoiceTrack)getHibernateTemplate().get(InvoiceTrack.class, id);
		return d;
	}
	
	public ExistentInvoice getExistentInvoice(int id){
		ExistentInvoice e =(ExistentInvoice)getHibernateTemplate().get(ExistentInvoice.class, id);
		return e;
	}
	
	
	
	public void addExistentInvoice(ExistentInvoice existentInvoice) {
		
		getHibernateTemplate().save(existentInvoice);
	}
	
	public void updateExistentInvoice(ExistentInvoice existentInvoice) {
		
		getHibernateTemplate().update(existentInvoice);
	}
	
	public void deleteExistentInvoice(int id){
		
		getHibernateTemplate().delete(getExistentInvoice(id));
	}
	
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerEIList(int start,int limit) {
		List <ExistentInvoice>list = getHibernateTemplate().find("from ExistentInvoice e  where e.flag is null" );
		ExistentInvoice e = new ExistentInvoice();
		e.setCompany("合计");
		for (ExistentInvoice existentInvoice : list) {
			e.setMoney(e.getMoney() + existentInvoice.getMoney());
			e.setTax(e.getTax() + existentInvoice.getTax());
		}
		int a = 0;
		double d ;
		a = (int) (e.getMoney() * 100);
		d = a;
		e.setMoney(d/100);
		a = (int) (e.getTax() * 100);
		d = a;
		e.setTax(d/100);
		list.add(e);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ExistentInvoice>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	

	@SuppressWarnings("unchecked")
	public List<InvoiceTrack> listInvoiceTrack() {
		return getHibernateTemplate().find("from InvoiceTrack");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <InvoiceTrack>list = getHibernateTemplate().find("from InvoiceTrack i  where i.endDate is null order by i.type" );
		InvoiceTrack i = new InvoiceTrack();
		i.setCompany("合计");
		for (InvoiceTrack invoiceTrack : list) {
			i.setMoney(i.getMoney() + invoiceTrack.getMoney());
			
		}
		int a = 0;
		double d ;
		a = (int) (i.getMoney() * 100);
		d = a;
		i.setMoney(d/100);
		list.add(i);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <InvoiceTrack>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String query ,InvoiceTrack i) {
		List <InvoiceTrack>list;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sql = "from InvoiceTrack i where ";
		int count = 0;
		if(query.equals("未终结")){
			sql += "i.endDate is null";
			count++;
		}else if(query.equals("已终结")){
			sql += "i.endDate is not null";
			count++;
		}
		
		if(!i.getCompany().equals("")){
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.company like '%" + i.getCompany() + "%'";
			count++;
		}
		if(!i.getType().equals("")){
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.type = '" + i.getType() + "'";
			count++;
		}
		if(i.getDate() != null && i.getEndDate() == null ){
			String d = sdf.format(i.getDate());
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.date >= '" + d + "'";
		}
		if(i.getEndDate() != null && i.getDate() == null ){
			String d = sdf.format(i.getEndDate());
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.date <= '" + d + "'";
		}
		if(i.getEndDate() != null && i.getDate() != null ){
			String d1 = sdf.format(i.getDate());
			String d2 = sdf.format(i.getEndDate());
			if (count > 0) {
				sql += " and ";
			}
			sql += "i.date between '" + d1 + "' and '" +d2  +"'";
		}
		if(query.equals("全部")&&count==0){
			sql = "from InvoiceTrack";
		}
		
		
		list = getHibernateTemplate().find(sql);
		InvoiceTrack it = new InvoiceTrack();
		it.setCompany("合计");
		for (InvoiceTrack invoiceTrack : list) {
			it.setMoney(it.getMoney() + invoiceTrack.getMoney());
			
		}
		int a = 0;
		double d ;
		a = (int) (it.getMoney() * 100);
		d = a;
		it.setMoney(d/100);
		list.add(it);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <InvoiceTrack>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	


}



